package cn.ym.user.controller;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.basic.utils.PageList;
import cn.ym.user.service.IUserService;
import cn.ym.user.domain.User;
import cn.ym.user.query.UserQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；用户
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
                userService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Long id) {
        return userService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<User> findAll(){
        return userService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }

    /**
     * 接口：手机号注册
     * @param
     * @return
     */
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody Map<String,String> map){


        return userService.phoneRegister(map);
    }


    /**
     * 接口：邮箱注册
     * @param
     * @return
     */
    @PostMapping("/register/email")
    public AjaxResult emailRegister(@RequestBody Map<String,String> map) throws MessagingException {

        userService.emailRegister(map);
        return new AjaxResult();
    }

    /**
     * 接口：邮箱注册---发送邮件---激活
     * @param
     * @return
     */
    @ApiOperation(value = "审核成功")
    @GetMapping("/active/{id}")
    public String active(@PathVariable Long id){

        userService.active(id);
        return "亲：激活成功,请点击"+"<a href='http://127.0.0.1/login.html'>【登录】</a>";
    }

    /**
     * 接口：查询单个对象
     * @param logininfoId
     */
    @GetMapping("/logininfoId/{logininfoId}")
    public User findBylogininfoId(@PathVariable("logininfoId")Long logininfoId) {

        return userService.findBylogininfoId(logininfoId);
    }

}
